<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-alert type="info" :showIcon="true">
      <div slot="message">
        <!-- 当前选择：<span v-if="selectedKeys != null && selectedKeys.length > 0">{{ getCurrSelectedTitle() }}</span>
        <a v-if="selectedKeys != null && selectedKeys.length > 0" style="margin-left: 10px" @click="onClearSelected"
          >取消选择</a
        > -->
        请选择需要关联的指标分类(至少选择一个)
      </div>
    </a-alert>
    <span style="user-select: none">
      <!-- <a-tree
        :replaceFields="replaceFields"
        @select="onSelect"
        @check="onCheck"
        :selectedKeys="selectedKeys"
        :checkedKeys="checkedKeys"
        :treeData="treeData"
        :checkStrictly="checkStrictly"
        :expandedKeys="iExpandedKeys"
        :autoExpandParent="autoExpandParent"
        @expand="onExpand"
      /> -->

      <a-tree
        :checkedKeys="checkedKeys"
        checkable
        :replaceFields="replaceFields"
        :selectedKeys="selectedKeys"
        :treeData="treeData"
        :expandedKeys="iExpandedKeys"
        :checkStrictly="checkStrictly"
        :autoExpandParent="autoExpandParent"
        @select="onSelect"
        @check="onCheck"
        @expand="onExpand"
      />
    </span>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
export default {
  name: 'AddIndexToSystemModal',
  props: {
  },
  data() {
    return {
      currSelected: {},
      indexIds: [],
      title: '选择指标分类',
      width: 800,
      visible: false,
      disableSubmit: false,
      replaceFields: {
        children: 'childList', title: 'catalogName', key: 'id'
      },
      selectedKeys: [],
      checkedKeys: [],
      iExpandedKeys: [],
      autoExpandParent: true,
      checkStrictly: true,
      treeData: []
    }
  },
  methods: {
    // getCurrSelectedTitle() {
    //   return !this.currSelected.catalogName ? '' : this.currSelected.catalogName
    // },
    onClearSelected() {
      // this.currSelected = {}
      this.checkedKeys = []
      this.selectedKeys = []
    },
    add(indexIds) {
      this.loadTree()
      this.indexIds = indexIds
      this.visible = true
    },
    loadTree(val) {
      var that = this
      that.treeData = []

      var queryParam = {}
      // if (val) {
      //   queryParam.catalogName = val
      // }
      getAction('/index/config/queryIndexCatalog', queryParam).then((res) => {
        if (res.success) {
          // 部门全选后，再添加部门，选中数量增多
          // this.allTreeKeys = []
          for (let i = 0; i < res.result.length; i++) {
            const temp = res.result[i]
            that.treeData.push(temp)
            // that.setThisExpandedKeys(temp)
            // that.getAllKeys(temp)
            // console.log(temp.id)
          }
          this.loading = false
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    close() {
      this.$emit('close')
      this.onClearSelected()
      this.visible = false
    },
    handleOk() {
      console.log(this.checkedKeys)
      if (this.checkedKeys == null || this.checkedKeys.length == 0) {
        this.$message.success('请选择指标分类!')
        return
      }

      const submitFormData = {
        catalogIdList: this.checkedKeys,
        indexIdList: this.indexIds
      }

      httpAction('/index/config/batchAddIndexCatalog', submitFormData, 'POST').then((res) => {
        if (res.success) {
          this.$message.success('保存成功!')
          this.close()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleCancel() {
      this.close()
    },
    onSelect(selectedKeys, info) {
      // const record = e.node.dataRef
      // this.currSelected = Object.assign({}, record)
      this.selectedKeys = selectedKeys
    },
    onCheck(checkedKeys) {
      console.log('onCheck', checkedKeys)
      this.checkedKeys = checkedKeys.checked
    },
    // onCheck(checkedKeys, info) {
    //   console.log('onCheck', checkedKeys, info)
    //   this.hiding = false
    //   // this.checkedKeys = checkedKeys.checked
    //   // <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
    //   if (this.checkStrictly) {
    //     this.checkedKeys = checkedKeys.checked
    //   } else {
    //     this.checkedKeys = checkedKeys
    //   }
    //   // <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
    // },
    onExpand(expandedKeys) {
      // console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    }
  }
}
</script>

<style>
</style>
